package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bean.Tunnel;



public class TunnelDaoimpl extends BaseDao implements TunnelDao{

	@Override
	public List<Tunnel> getList(Tunnel tu) {
		Connection conn = null;
		PreparedStatement pstme = null;
		ResultSet rs = null;
		List<Tunnel> list = new ArrayList<>();
		
		try {
			conn = this.getConnection();
			String sql = "SELECT luduan,suidao,zhuangtao,changdu,kuan,gao,xiujianniandu,gaijianniandu,gaijianname,shejiname,shejiname,jianliname,tongcheshijian,guanyangname,jishudengji,pingdingriqi,pingdingdanwei,zhengqudaima,beizhu from Tunnel where 1=1 ";
			if(tu!=null&&tu.getLuduan()!=0){
				sql = sql + " and luduan="+tu.getLuduan();
			}
			 if(tu!=null&&tu.getSuidao()!=null&&!"".equals(tu.getSuidao())){
				sql = sql + " and suidao='"+tu.getSuidao()+"'";
			}
			pstme = conn.prepareStatement(sql);
			rs = pstme.executeQuery();
			while(rs.next()){
				Tunnel a = new Tunnel();
				a.setLuduan(rs.getInt(1));
				a.setSuidao(rs.getString(2));
				a.setZhuangtao(rs.getInt(3));
				a.setChangdu(rs.getInt(4));
				a.setKuan(rs.getInt(5));
				a.setGao(rs.getInt(6));
				a.setXiujianniandu(rs.getString(7));
				a.setGaijianniandu(rs.getString(8));
				a.setGaijianname(rs.getString(9));
				a.setShejiname(rs.getString(10));
				a.setShigongname(rs.getString(11));
				a.setJianliname(rs.getString(12));
				a.setTongcheshijian(rs.getString(13));
				a.setGuanyangname(rs.getString(14));
				a.setJishudengji(rs.getString(15));
				a.setPingdingriqi(rs.getString(16));
				a.setPingdingdanwei(rs.getString(17));
				a.setZhengqudaima(rs.getString(18));
				a.setBeizhu(rs.getString(13));
				list.add(a);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstme, rs);
		}
		
		return list;
	}

	@Override
	public int add(Tunnel tu) {
		String sql = "insert into Tunnel(luduan,suidao,zhuangtao,changdu,kuan,gao,xiujianniandu,gaijianniandu,gaijianname,shejiname,shigongname,jianliname,tongcheshijian,guanyangname,jishudengji,pingdingriqi,pingdingdanwei,zhengqudaima,beizhu) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = {tu.getLuduan(),tu.getSuidao(),tu.getZhuangtao(),tu.getChangdu(),tu.getKuan(),tu.getGao(),tu.getXiujianniandu(),tu.getGaijianname(),tu.getShejiname(),tu.getShigongname(),tu.getJianliname(),tu.getTongcheshijian(),tu.getGuanyangname(),tu.getJishudengji(),tu.getPingdingriqi(),tu.getPingdingdanwei(),tu.getZhengqudaima(),tu.getBeizhu()};
		int count = this.executeUpdate(sql, params);
		return count;
	}
	@Override
	public int update(Tunnel tu) {
		String sql = "UPDATE Tunnel SET  luduan=?,suidao=?,zhuangtao=?,changdu=?,kuan=?,gao=?,xiujianniandu=?,gaijianniandu=?,gaijianname=?,shejiname=?,shigongname=?,jianliname=?,tongcheshijian=?,guanyangname=?,jishudengji=?,pingdingriqi=?,pingdingdanwei=?,zhengqudaima=?,beizhu=? WHERE suidao=?";
		Object[] params = {tu.getLuduan(),tu.getSuidao(),tu.getZhuangtao(),tu.getChangdu(),tu.getKuan(),tu.getGao(),tu.getXiujianniandu(),tu.getGaijianname(),tu.getShejiname(),tu.getShigongname(),tu.getJianliname(),tu.getTongcheshijian(),tu.getGuanyangname(),tu.getJishudengji(),tu.getPingdingriqi(),tu.getPingdingdanwei(),tu.getZhengqudaima(),tu.getBeizhu(),tu.getSuidao()};
		int count = this.executeUpdate(sql, params);
		return count;
	}

	@Override
	public int del(int id) {
		String sql = "DELETE FROM Tunnel WHERE suidao=?";
		Object[] params = {id};
		int count = this.executeUpdate(sql, params);
		return count;
	}


}
